{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "53fa30f2",
   "metadata": {},
   "source": [
    "1.`np.array()` —— 从现有对象创建 NumPy 数组\n",
    "\n",
    "功能：\n",
    "`np.array()` 用于从现有的 Python 数据结构（如列表、元组、或其他数组）创建 NumPy 数组。它是最常见的创建数组的方式。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21400217",
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "# np.array\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "# 从一个列表创建数组\n",
    "arr = np.array([1,2,3,4])\n",
    "print(arr)\n",
    "\n",
    "# 创建一个二维数组\n",
    "arr_2d = np.array([[1,2,3],[4,5,6]])\n",
    "print(arr_2d)\n",
    "\n",
    "# 指定数据类型\n",
    "arr_float = np.array([1,2,3],dtype=float)\n",
    "print(arr_float)\n",
    "\n",
    "\n",
    "# 指定最小维度\n",
    "arr_3d = np.array([1,2,3,4],ndmin=3)\n",
    "print(arr_3d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da8d80a2",
   "metadata": {},
   "source": [
    "2.`np.asarray()` —— 从现有对象创建数组（不复制）\n",
    "\n",
    "功能：\n",
    "`np.asarray()` 也是用于从现有对象创建 NumPy 数组。与 `np.array()` 类似，但它有一个重要的区别：如果输入已经是 NumPy 数组，`np.asarray()` 不会创建副本，而是直接返回原始数组。这使得它在某些情况下更加高效。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b66b7e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.asarray\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "# 从一个列表创建数组\n",
    "arr = np.asarray([1, 2, 3, 4])\n",
    "print(arr)  # 输出: [1 2 3 4]\n",
    "\n",
    "# 从一个已经是 NumPy 数组的对象创建数组\n",
    "arr_existing = np.array([1, 2, 3, 4])\n",
    "arr_from_existing = np.asarray(arr_existing)\n",
    "print(arr_from_existing is arr_existing)  # 输出: True\n",
    "\n",
    "# 指定数据类型\n",
    "arr_float = np.asarray([1, 2, 3], dtype=float)\n",
    "print(arr_float)  # 输出: [1. 2. 3.]\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
